package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * 输入用户名, 删除对应的用户
 */
public class Test {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                    "root",
                    "root"
            );
            System.out.println("与数据库连接成功");
            Statement statement = connection.createStatement();

            Scanner sc = new Scanner(System.in);
            System.out.println("请输入想要删除的用户的用户名:");
            String username = sc.nextLine();
            //DELETE FROM userinfo WHERE username='';
            String sql = "DELETE FROM userinfo WHERE username='"+username+"';";
            int num = statement.executeUpdate(sql);
            if (num>0)
                System.out.println("用户删除成功");
            else
                System.out.println("用户删除失败");

            connection.close();
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
}
